﻿#include "UnstructuredGridReader.h"
#include <vtksys/SystemTools.hxx>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>

namespace pst
{

UnstructuredGridReader::UnstructuredGridReader(QObject *parent)
    : QObject(parent)
    , m_fileName{}
    , m_reader(vtkUnstructuredGridReader::New())
{

}

UnstructuredGridReader::~UnstructuredGridReader()
{
    m_reader->Delete();
}

void UnstructuredGridReader::SetFileName(const QString &fileName)
{
    m_fileName = fileName;
}

QString UnstructuredGridReader::GetFileName()
{
    return m_fileName;
}

bool UnstructuredGridReader::Update()
{
    if (!vtksys::SystemTools::FileExists(m_fileName.toStdString(), true))
    {
        std::cout << "Read unstructed grid fail!" << std::endl;
        std::cout << "File name is "<<m_fileName.toStdString()<<std::endl;
        return false;
    }
    m_reader->SetFileName(m_fileName.toStdString().c_str());
    m_reader->ReadAllVectorsOn();
    m_reader->ReadAllScalarsOn();
    m_reader->ReadAllFieldsOn();
    try
    {
        m_reader->Update();
    }
    catch (const std::exception& e)
    {
        std::cout<<e.what()<<std::endl;
        return false;
    }

    return true;
}

vtkUnstructuredGrid *UnstructuredGridReader::GetOutput()
{
    return m_reader->GetOutput();
}

}
